<template>
  <div>
    <a-form ref="queryConfigFormRef" name="queryConfigForm" :model="queryConfigForm.data" :labelCol="{ span: 8 }" :wrapperCol="{ span: 14 }">
      <a-row :gutter="24">
        <a-col span="8">
          <a-form-item name="configName" label="参数名称">
            <a-input v-model:value="queryConfigForm.data.configName" allowClear></a-input>
          </a-form-item>
        </a-col>
        <a-col span="8">
          <a-form-item name="configKey" label="参数键名">
            <a-input v-model:value="queryConfigForm.data.configKey" allowClear></a-input>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="8">
          <a-form-item name="beginTime" label="创建时间起期">
            <a-date-picker v-model:value="queryConfigForm.data.params.beginTime" format="YYYY-MM-DD" valueFormat="YYYY-MM-DD" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item name="endTime" label="创建时间止期">
            <a-date-picker v-model:value="queryConfigForm.data.params.endTime" format="YYYY-MM-DD" valueFormat="YYYY-MM-DD" />
          </a-form-item>
        </a-col> -->
      </a-row>
      <a-row :gutter="24" class="autoee-button-row">
        <a-col :span="12" style="text-align: left">
          <a-button type="primary" @click="openConfigAddModal()" v-hasPermi="['system:config:add']"><AutoeeIcon icon="plus-outlined" />新增</a-button>
          <a-button type="primary" danger @click="clickDeleteConfig()" :disabled="configTableData.selectedRowKeys.length <= 0" v-hasPermi="['system:config:remove']"><AutoeeIcon icon="delete-outlined" />删除</a-button>
          <a-button type="primary" @click="clickExport()" v-hasPermi="['system:config:export']"><AutoeeIcon icon="export-outlined" />导出</a-button>
        </a-col>
        <a-col :span="12" style="text-align: right">
          <a-button type="primary" @click="configTablePage.reload()"> <AutoeeIcon icon="SearchOutlined" />查询 </a-button>
          <a-button @click="resetConfigQueryForm()"><AutoeeIcon icon="redo-outlined" />重置</a-button>
        </a-col>
      </a-row>
    </a-form>

    <a-table
      rowKey="configId"
      :columns="configTableData.columns"
      :data-source="configTableData.dataSource"
      :loading="configTablePage.loading.value"
      :pagination="configTablePagination"
      @change="configTableHandChangePage"
      :row-selection="{ selectedRowKeys: configTableData.selectedRowKeys, onChange: onConfigTableSelectChange }"
      bordered
      size="small"
      tableLayout="fixed"
    >
      <template #bodyCell="{ index, column, record }">
        <template v-if="column.key === 'pageIndex'">
          {{ index + 1 }}
        </template>
        <template v-else-if="column.key === 'configType'">
          <span>
            {{ AutoeeUtil.getDictLabelByKey(record.configType, pageData.sysYesNoDict) }}
          </span>
        </template>
        <template v-else-if="column.key === 'configTableOperateCol'">
          <span>
            <a @click="openConfigDetailModal(record)"> 查看 </a>
            <a-divider type="vertical" v-hasPermi="['system:config:edit']" />
            <a @click="openConfigUpdateModal(record)" v-hasPermi="['system:config:edit']"> 修改 </a>
          </span>
        </template>
      </template>
    </a-table>

    <ConfigAddUpdateModal ref="configAddUpdateModalRef" :sysYesNoDict="pageData.sysYesNoDict" />
    <ConfigDetailModal ref="configDetailModalRef" :sysYesNoDict="pageData.sysYesNoDict" />
  </div>
</template>

<script setup name="Config">
import { listConfig, getConfig, delConfig, addConfig, updateConfig } from '@/api/system/config';
import ConfigAddUpdateModal from './addUpdateModal.vue';
import ConfigDetailModal from './detailModal.vue';
import { computed, reactive, ref } from 'vue';
import { AutoeeIcon } from '@/utils/AutoeeIcon.js';
import AutoeeUtil from '@/utils/AutoeeUtil.js';
import { message } from 'ant-design-vue';

// 当前页面使用的数据
const pageData = reactive({
  sysYesNoDict: [],
});

AutoeeUtil.getDictsByType('sys_yes_no').then((res) => {
  pageData.sysYesNoDict = res;
});

// 查询Form
const queryConfigFormRef = ref();
const queryConfigForm = reactive({ data: { params: {} } });

// 重置查询Form
const resetConfigQueryForm = () => {
  AutoeeUtil.resetFormFieldsToEmpty(queryConfigForm.data);
  configTablePage.reload();
};

// 打开新增窗口
const configAddUpdateModalRef = ref();
const openConfigAddModal = () => {
  configAddUpdateModalRef.value.openAddModal(configTablePage);
};

// 打开修改窗口
const openConfigUpdateModal = (record) => {
  configAddUpdateModalRef.value.openUpdateModal(record, configTablePage);
};

// 打开详细窗口
const configDetailModalRef = ref();
const openConfigDetailModal = (record) => {
  configDetailModalRef.value.openModal(record);
};

// 点击删除
const clickDeleteConfig = () => {
  AutoeeUtil.confirmDeleteSelectedData(() => {
    delConfig(configTableData.selectedRowKeys).then((res) => {
      AutoeeUtil.messageSuccess('删除操作成功。');
      configTablePage.reload();
    });
  });
};

// 点击导出
const clickExport = () => {
  AutoeeUtil.download('system/config/export', queryConfigForm.data, `config_${new Date().getTime()}.xlsx`);
};

// 列表数据
const configTableData = reactive({
  // 列表数据
  dataSource: [],
  // 列表记录数
  total: 0,
  selectedRowKeys: [],
  // 列表列定义
  columns: [
    {
      title: '序号',
      dataIndex: 'pageIndex',
      key: 'pageIndex',
      width: 50,
      align: 'center',
    },
    {
      title: '参数名称',
      key: 'configName',
      dataIndex: 'configName',
    },
    {
      title: '参数键名',
      key: 'configKey',
      dataIndex: 'configKey',
    },
    {
      title: '参数键值',
      key: 'configValue',
      dataIndex: 'configValue',
    },
    {
      title: '系统内置',
      key: 'configType',
      dataIndex: 'configType',
    },
    {
      title: '创建时间',
      key: 'createTime',
      dataIndex: 'createTime',
    },
    {
      title: '操作',
      key: 'configTableOperateCol',
      width: 100,
    },
  ],
});

// 查询用户列表
const queryConfigList = () => {
  return listConfig(queryConfigForm.data);
};

// 用户列表翻页数据：使用vue-request中分页方法，返回参数包含{ run, reload, loading, current, pageSize }
const configTablePage = AutoeeUtil.usePagination(queryConfigList, configTableData);

// 用户列表翻页工具条：必须通过计算函数每次重新生成
const configTablePagination = computed(() => AutoeeUtil.getTablePagination(configTablePage));

// 手动翻页方法
const configTableHandChangePage = (page, filters, sorter) => {
  AutoeeUtil.tableHandChangePage(configTablePage, page, filters, sorter);
};

// 列表选中方法
const onConfigTableSelectChange = (selectedRowKeys, selectedRows) => {
  configTableData.selectedRowKeys = selectedRowKeys;
  // console.log('selectedRows[0].dictType=' + JSON.stringify(selectedRows[0].dictType));
};
</script>
<style lang="less"></style>
